package joaomarceloos.scribblerapp.utils;

import java.util.ArrayList;

import joaomarceloos.scribblerapp.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GroupArrayAdapter extends ArrayAdapter<Group>{

    Context context;
    int layoutResourceId;   
    ArrayList<Group> data;
   
    public GroupArrayAdapter(Context context, int layoutResourceId, ArrayList<Group> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        GroupHolder holder;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new GroupHolder();
            holder.txtName = (TextView)row.findViewById(R.id.txtItemRowName);
            holder.txtAddr = (TextView)row.findViewById(R.id.txtItemRowAddr);
            // holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
           
            row.setTag(holder);
        }
        else
        {
            holder = (GroupHolder)row.getTag();
        }
       
        Group group = data.get(position);
        holder.txtName.setText(group.name);
        holder.txtAddr.setText(group.addr.getHostAddress());
        // holder.imgIcon.setImageResource(client.icon);
       
        return row;
    }
    
    static class GroupHolder
    {
        //ImageView imgIcon;
        TextView txtName, txtAddr;
    }
}